
import uploadImage from '@/common/js/ossutil/uploadFile.js'
/*
 * 视频直传oss
 * @param object opt
 * @param callable successCallback 成功执行方法 data 
 * @param callable errorCallback 失败执行方法 
 */
export function uploadVideoOSS(opt, successCallback, errorCallback) {
	let that = this;
	if (typeof opt === 'string') {
		let url = opt;
		opt = {};
		opt.url = url;
	}
	// 选择视频参数
	let chooseOpt = {
		sizeType: opt.sizeType || ['compressed'],
		sourceType: opt.sourceType || ['album', 'camera'],
		is_load: opt.is_load || true,
		uploadUrl: opt.url || '',
		inputName: opt.name || 'field'
	}

	// oss上传所需
	let ossOpt = {
		dir: 'mini/' + opt.dir || "video"
	}
	let key = uni.getStorageSync('imgUploadKey')

	// 选择图片上传
	let chooseFilesUpload = function(chooseOpt) {

		uni.chooseVideo({
			sourceType: chooseOpt.sourceType, // 可以指定来源是相册还是相机，默认二者都有  
			success: function(res) {
				//启动上传等待中...  
				console.log("视频信息",res)
				uni.showLoading({
					title: '视频上传中',
				});
				// let imgUploadKey = JSON.parse(uni.getStorageSync('imgUploadKey'))
				// let UID = uni.getStorageSync('UID')
				// let STORE_ID = uni.getStorageSync('STORE_ID')

				// let str = res.tempFilePath.substr(res.tempFilePath.lastIndexOf('.'));
				// let timer = new Date()
				// let imgUrl = UID + timer.getTime() + str;

				// console.log("imgUploadKey", imgUploadKey)
				let sizeBase = 1024 * 1024
				if(opt.size && res.size > opt.size * sizeBase) {
					uni.hideLoading()
					uni.showModal({
						title: `视频请勿超过60s,`,
						content: '请录制后重试',
						showCancel: false,
					})
					return;
				}
				
				uploadImage(res.tempFilePath, 'feedback/',
					result => {
						console.log('打印的结果', result);
						
						successCallback && successCallback(result)
						// that.photo.push(result)
						uni.hideLoading()
					}, error => {
						uni.hideLoading()
						uni.showToast({
							title: '上传失败',
							image: '../../static/authentication/error.png'
						})
					})
				
				// uni.uploadFile({
				// 	url: imgUploadKey.host,
				// 	filePath: res.tempFilePath,
				// 	name: 'file', // 必须写这个
				// 	formData: {
				// 		key: ossOpt.dir + '/' + nameStr,
				// 		policy: imgUploadKey.policy,
				// 		OSSAccessKeyId: imgUploadKey.OSSAccessKeyId,
				// 		signature: imgUploadKey.signature,
				// 	},
				// 	header: {
				// 		// #ifdef MP
				// 		"Content-Type": "multipart/form-data",
				// 		// #endif
				// 		[TOKENNAME]: 'Bearer ' + store.state.app.token
				// 	},
				// 	success: (res) => {
				// 		let STORE_ID = uni.getStorageSync('STORE_ID')
				// 		uni.hideLoading();
				// 		if (res.statusCode == 403) {
				// 			that.Tips({
				// 				title: res.data
				// 			});
				// 		} else {
				// 			let data = res.statusCode
				// 			if (res.statusCode == 204) {
				// 				let url = imgUploadKey.host + '/' + ossOpt.dir +
				// 					'/' + nameStr;
				// 				successCallback && successCallback(url)
				// 			} else {
				// 				errorCallback && errorCallback(data);
				// 				that.Tips({
				// 					title: data.message
				// 				});
				// 			}
				// 		}
				// 	},
				// 	fail: function(res) {
				// 		uni.hideLoading();
				// 		that.Tips({
				// 			title: '上传视频失败'
				// 		});
				// 	}
				// })
			}
		})
	}

	// let ossSignature = function(ossOpt, chooseOpt) {
	// 	// 获取oss密钥
	// 	uploadSignature(ossOpt).then(res => {
	// 		let imgUploadKey = res.data.signature
	// 		uni.setStorageSync('imgUploadKey', JSON.stringify(imgUploadKey))
	// 	});
	// }

	// ossSignature(ossOpt, chooseOpt)

	chooseFilesUpload(chooseOpt)
}


export function uploadPhotoOSS(opt, successCallback, errorCallback) {
	let that = this;
	if (typeof opt === 'string') {
		let url = opt;
		opt = {};
		opt.url = url;
	}
	// 选择视频参数
	let chooseOpt = {
		count: opt.count ||  1 ,
		sizeType: opt.sizeType || ['compressed'],
		sourceType: opt.sourceType || ['album', 'camera'],
		is_load: opt.is_load || true,
		uploadUrl: opt.url || '',
		inputName: opt.name || 'field'
	}

	// oss上传所需
	let ossOpt = {
		dir: 'mini/' + opt.dir || "img"
	}
	let key = uni.getStorageSync('imgUploadKey')

	// 选择图片上传
	let chooseFilesUpload = function(chooseOpt) {

		uni.chooseImage({
			sourceType: chooseOpt.sourceType, // 可以指定来源是相册还是相机，默认二者都有  
			success: function(res) {
				//启动上传等待中...  
				console.log("图片信息",res)
				uni.showLoading({
					title: '图片上传中',
				});
				// let imgUploadKey = JSON.parse(uni.getStorageSync('imgUploadKey'))
				// let UID = uni.getStorageSync('UID')
				// let STORE_ID = uni.getStorageSync('STORE_ID')

				// let str = res.tempFilePath.substr(res.tempFilePath.lastIndexOf('.'));
				// let timer = new Date()
				// let imgUrl = UID + timer.getTime() + str;

				// console.log("imgUploadKey", imgUploadKey)
				let sizeBase = 1024 * 1024
				if(opt.size && res.size > opt.size * sizeBase) {
					uni.hideLoading()
					uni.showModal({
						title: `图片请勿超过5MB,`,
						content: '请拍照后重试',
						showCancel: false,
					})
					return;
				}
				
				uploadImage(res.tempFilePaths[0], 'feedback/',
					result => {
						console.log('打印的结果', result);
						
						successCallback && successCallback(result)
						// that.photo.push(result)
						uni.hideLoading()
					}, error => {
						console.log('打印的结果', error);
						
						uni.hideLoading()
						uni.showToast({
							title: '上传失败',
							image: '../../static/authentication/error.png'
						})
					})
				
				// uni.uploadFile({
				// 	url: imgUploadKey.host,
				// 	filePath: res.tempFilePath,
				// 	name: 'file', // 必须写这个
				// 	formData: {
				// 		key: ossOpt.dir + '/' + nameStr,
				// 		policy: imgUploadKey.policy,
				// 		OSSAccessKeyId: imgUploadKey.OSSAccessKeyId,
				// 		signature: imgUploadKey.signature,
				// 	},
				// 	header: {
				// 		// #ifdef MP
				// 		"Content-Type": "multipart/form-data",
				// 		// #endif
				// 		[TOKENNAME]: 'Bearer ' + store.state.app.token
				// 	},
				// 	success: (res) => {
				// 		let STORE_ID = uni.getStorageSync('STORE_ID')
				// 		uni.hideLoading();
				// 		if (res.statusCode == 403) {
				// 			that.Tips({
				// 				title: res.data
				// 			});
				// 		} else {
				// 			let data = res.statusCode
				// 			if (res.statusCode == 204) {
				// 				let url = imgUploadKey.host + '/' + ossOpt.dir +
				// 					'/' + nameStr;
				// 				successCallback && successCallback(url)
				// 			} else {
				// 				errorCallback && errorCallback(data);
				// 				that.Tips({
				// 					title: data.message
				// 				});
				// 			}
				// 		}
				// 	},
				// 	fail: function(res) {
				// 		uni.hideLoading();
				// 		that.Tips({
				// 			title: '上传视频失败'
				// 		});
				// 	}
				// })
			}
		})
	}

	// let ossSignature = function(ossOpt, chooseOpt) {
	// 	// 获取oss密钥
	// 	uploadSignature(ossOpt).then(res => {
	// 		let imgUploadKey = res.data.signature
	// 		uni.setStorageSync('imgUploadKey', JSON.stringify(imgUploadKey))
	// 	});
	// }

	// ossSignature(ossOpt, chooseOpt)

	chooseFilesUpload(chooseOpt)
}